package com.egrid.point.bury.advice;

import com.egrid.point.bury.code.Response;
import com.egrid.point.bury.code.SimpleResponse;
import com.egrid.point.bury.exception.BuryException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * BuryAdvice.java
 * Created at 2019-12-27
 * Created by Royal Warmer
 * Copyright (C) 2018 SAIC VOLKSWAGEN, All rights reserved.
 */
@RestControllerAdvice
@Slf4j
public class BuryAdvice {


    @Autowired
    private HttpServletResponse response;


    @ExceptionHandler(BuryException.class)
    public Response buryException(BuryException ex) {
        log.error(ex.getMessage(), ex);
        response.setStatus(500);
        return SimpleResponse.fail(ex.getMessage());
    }

    @ExceptionHandler({RuntimeException.class, Exception.class})
    public Response exception(Exception ex) {
        log.error(ex.getMessage(), ex);
        response.setStatus(500);
        return SimpleResponse.fail("系统错误");
    }


}
